package com.example.drools.spring.yc.util;

import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.stereotype.Component;

/**
 * @author ddsyw
 * @version 1.0
 * @date 2021/12/31 2:48 下午
 */
@Component
public class BeanContextUtil implements ApplicationContextAware {

  private static ApplicationContext applicationContext;

  @Override
  public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
    BeanContextUtil.applicationContext = applicationContext;
  }


  public static <T> T getBean(Class<T> clazz) {
   return (T) applicationContext.getBean(clazz);
  }

  public static <T> T getBean(String beanName) {
    return (T) applicationContext.getBean(beanName);
  }

  public static <T> T getBean(String beanName, Class<T> tClass) {
    return (T) applicationContext.getBean(beanName, tClass);
  }

}
